<script setup>
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// ------------------------------用户搜索
const formInline = reactive({
    userId: '',
    username: '',
    nickname: '',
    regst: 'all',
    reg: 'all',
    hot: 'all',
    dis: 'all',
    sup: 'all',
    dic: 'all',
    regTime: ''
})

const onSubmit = () => {
    console.log('submit!')
}
// ------------------------------用户数据
let users = reactive([
    {
        id: 194, name: '李汉玲（422）', money: '30.00', jidou: '3000', zengsong: '0', num: '648464657764',
        paytype: '微信', env: 'App', others: '', status: '未支付', times: '2024-07-09 16:16:25'
    },
    {
        id: 193, name: '贾志强（38225）', money: '6.00', jidou: '600', zengsong: '0', num: '687484654894325',
        paytype: '支付宝', env: 'App', others: '', status: '未支付', times: '2024-06-01 14:53:34'
    },
    {
        id: 192, name: '独恋天山雪（37755）', money: '6.00', jidou: '600', zengsong: '0', num: '45464134848',
        paytype: '支付宝', env: 'App', others: '', status: '未支付', times: '2024-08-09 16:12:25'
    },
    {
        id: 191, name: '独恋天山雪（37755）', money: '98.00', jidou: '10000', zengsong: '0', num: '18676464654',
        paytype: '支付宝', env: 'App', others: '', status: '未支付', times: '2024-07-10 19:16:35'
    },
    {
        id: 190, name: '梁东玲（2159）', money: '30.00', jidou: '3000', zengsong: '0', num: '68741549953254',
        paytype: '微信', env: 'App', others: '', status: '未支付', times: '2024-02-09 16:19:24'
    },
    {
        id: 190, name: '梁东玲（2159）', money: '30.00', jidou: '3000', zengsong: '0', num: '13454532128645',
        paytype: '支付宝', env: 'App', others: '', status: '未支付', times: '2024-02-09 17:29:54'
    },
    {
        id: 188, name: 'myx（44）', money: '30.00', jidou: '3000', zengsong: '0', num: '15648974654',
        paytype: '支付宝', env: 'App', others: '', status: '未支付', times: '2024-09-09 17:17:25'
    },
    
])

// ------------------------ 分页

const currentPage = ref(1)
const pageSize = ref(100)
const size = ref('default')
const background = ref(false)
const disabled = ref(false)

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
}

// ---------------------- 删除用户操作
// 弹出二次确认对话框：参数user就是要删除的用户：
function showConfirm(user) {
    ElMessageBox.confirm(
    '确定要删除 ' + user.name +' 用户的充值记录吗?',
    '警告信息',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 调用删除函数：删除用户
      deleteUser(user)
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '管理员取消了删除操作',
      })
    })
    
}

// 删除用户的函数：参数user就是要删除的用户
function deleteUser(user) {
    // 查询用户在数组中的索引
    let index = users.findIndex(item => item.id === user.id)
    // 删除用户数据
    users.splice(index, 1)
    // 提示删除成功
    ElMessage({
        type: 'success',
        message: '管理员成功删除了用户 ' + user.name + '的一条充值记录',
      })
}

// // ------------------ 新增用户操作 
// import DialogUserCreate from '../components/DialogUserCreate.vue'
// // 声明变量控制弹窗显示/隐藏
// let userDialogCreate = ref(false)
// // 准备函数：显示对话框
// function showCreateDialog() {
//     userDialogCreate.value = true
// }
// // 准备函数：隐藏对话框
// function hideCreateDialog() {
//     userDialogCreate.value = false
// }
// // 新增用户数据：参数user就是子组件告诉父组件要新增的用户
// function createUser(user) {
//     // 计算新用户的id
//     let id = users[0].id + 1
//     // 复制一份用户数据
//     let newUser = {...user}
//     // 给新数据添加id属性
//     newUser.id = id
//     // 新增用户
//     users.unshift(newUser)
// }


</script>
<template>
    <div class="p-header">
        <h2>充值管理</h2>
        <el-button type="primary" @click="showCreateDialog">新增充值记录</el-button>
    </div>
    <!-- 新增用户弹窗 -->
    <DialogUserCreate :dialogFormVisible="userDialogCreate"
        @createUserEvent="createUser"
        @closeCreateEvent="hideCreateDialog"></DialogUserCreate>

    <el-divider>充值记录搜索</el-divider>
    <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="订单状态">
                <el-select v-model="formInline.reg" placeholder="please select your zone">
                    <el-option label="全部" value="all" />
                    <el-option label="已支付" value="open" />
                    <el-option label="未支付" value="close" />
                </el-select>
            </el-form-item>
            <el-form-item label="提交时间">
                <el-date-picker
                    v-model="formInline.regTime"
                    type="daterange"
                    range-separator="To"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                />
            </el-form-item>
            <el-form-item label="用户ID：">
                <el-input v-model="formInline.userId" placeholder="请输入用户ID" clearable />
            </el-form-item>
            <el-form-item label="昵称：">
                <el-input v-model="formInline.nickname" placeholder="请输入会员昵称" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
            </el-form-item>
        </el-form>
    </el-card>
    <el-divider>充值信息展示</el-divider>
    <el-table :data="users" style="width: 100%">
        <el-table-column label="ID" prop="id" width="100"/>
        <el-table-column label="会员" prop="name" width="220"/>
        <el-table-column label="人民币金额" prop="money" width="150"/>
        <el-table-column label="兑换吉豆" prop="jidou" width="150"/>
        <el-table-column label="赠送吉豆" prop="zengsong" width="150"/>
        <el-table-column label="商户订单号" prop="num" width="250"/>
        <el-table-column label="支付类型" prop="paytype" width="150"/>
        <el-table-column label="支付环境" prop="env" width="150"/>
        <el-table-column label="第三方支付订单号" prop="others" width="250"/>
        <el-table-column label="订单状态" prop="status" width="150"></el-table-column>
        <el-table-column label="提交时间" prop="times" width="220"/>
        
        <!-- id: 194, name: '李汉玲（422）', money: '30.00', jidou: '3000', zengsong: '0', num: '648464657764',
        paytype: '微信', env: 'App', others: '', status: '未支付', times: '2024-07-09 16:16:25' -->
        
        <el-table-column align="left" fixed="right" width="220" label="操作">
            <template #default="scope">
                <el-button-group>
                    <el-button type="success" size="small">编辑</el-button>
                    <el-button type="danger" size="small" @click="showConfirm(scope.row)">删除</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination
      background
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[100, 200, 300, 400]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="1000"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</template>
<style scoped>
.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 100px;
    font-size: 14px;
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

.el-pagination{
    margin-top:20px;
}

.p-header{
    display:flex;
    justify-content: space-between;
}
</style>